package com.category.douyijia.dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.category.douyijia.model.ShopUser;
import com.category.douyijia.model.User;
import com.jfinal.aop.Aop;

/**
 * 用户店铺Dao功能实现，
 * 实现了基本的店铺管理功能
 * @author JunfengLiu
 * @version v1.0
 * date time:2021-8-6 15:53:20
 */
public class ShopUserDao {
	/**
	 * 创建依赖注入对象user
	 */
	private User user = Aop.get(User.class);
	/**
	 * 创建依赖注入对象shopUser
	 */
	private ShopUser shopUser = Aop.get(ShopUser.class);
	/**
	 * 创建user集合对象
	 */
	private List<User> userList = new ArrayList<User>();
	/**
	 * 创建shopUser集合对象
	 */
	private List<ShopUser> shopUserList = new ArrayList<ShopUser>();
	/**
	 * 创建强关联性的map对象
	 */
	private Map<User, ShopUser> shopofUserMap = new HashMap<User, ShopUser>();
	/**
	 * 获取用户集合
	 * @return
	 */
	public List<User> getUserList() {
		return userList;
	}
	/**
	 * 设置用户集合
	 * @param userList
	 */
	public void setUserList(List<User> userList) {
		this.userList = userList;
	}
	public List<ShopUser> getShopUserList() {
		return shopUserList;
	}
	public void setShopUserList(List<ShopUser> shopUserList) {
		this.shopUserList = shopUserList;
	}
	public Map<User, ShopUser> getShopofUserMap() {
		return shopofUserMap;
	}
	public void setShopofUserMap(Map<User, ShopUser> shopofUserMap) {
		this.shopofUserMap = shopofUserMap;
	}
	/**
	 * 保存返回增、删、改的结果
	 */
	private boolean result;
	/**
	 * 查询所有店铺信息，
	 * 无用户约束权限，
	 * 开发者可使用
	 * @return
	 * 店铺集合
	 */
	public List<ShopUser> selectAll(){
		shopUserList = shopUser.dao().findAll();
		return shopUserList;
	}
	/**
	 * 通过用户ID查找店铺信息
	 * @param userId
	 * 用户ID编号
	 * @return
	 * 用户信息集合
	 */
	public List<ShopUser> selectShopByUserId(String userId){
		//清空集合
		shopUserList = null;
		//判断用户ID长度是否大于0，防止注入攻击
		if (userId.trim().length()>0) {
			//查找用户信息
			user = user.dao().findById(userId);
		}
		//判断用户是否为空
		if (user != null) {
			//不为空传递集合信息
			shopUserList = shopUser.dao().find("select * from "
												+ "t_shop_user where "
												+ "login_user_id = ?"
												, userId);
		}
		return shopUserList;
	}
	/**
	 * 通过店铺ID查找店铺信息
	 * @param shopId
	 * 店铺ID
	 * @return
	 * 店铺信息
	 */
	public ShopUser selectShopUserById(String shopId){
		shopUser = null;
		if (shopId.trim().length() > 0) {
			shopUser = shopUser.dao().findById(shopId);
		}
		return shopUser;
	}
	/**
	 * 根据用户ID添加店铺信息
	 * @param userId
	 * 用户ID
	 * @param shop
	 * 店铺信息
	 * @return
	 * 添加结果
	 */
	public boolean insertShop(String userId,ShopUser shop){
		result = false;
		if (userId.trim().length() > 0 && shop != null) {
			shopUser = shop;
		}
		if (userId.equals(shopUser.getLoginUserId())) {
			result = shopUser.dao()._setAttrs(shop).save();
		}
		return result;
	}
	/**
	 * 更新店铺信息
	 * @param shopId
	 * 店铺ID
	 * @param newShop
	 * 新的店铺信息
	 * @return
	 * 更新结果
	 */
	public boolean updateShopByUserId(String shopId,ShopUser newShop){
		result = false;
		//创建接收原始用户信息的实体类
		ShopUser oldShop = new ShopUser();
		if (shopId.trim().length() >0) {
			/*
			 * 通过用户店铺id进行信息查找。
			 * 除店铺所有者之外的其他人员禁止修改店铺，
			 * 仅有查看权限。
			 */
			oldShop = oldShop.dao().findById(shopId);
		}
		if (oldShop != null) {
			shopUser = oldShop;
		}
		//判断新老店铺信息是否相同，同时判断新旧店铺id是否相同
		if (shopUser != newShop 
				&& 
			newShop.getLoginUserId().equals(shopUser.getLoginUserId())) {
			result = shopUser.dao()
							.findById(shopId)
							._setOrPut(newShop)
							.update();
			return result;
		}
		return result;
	}
	/**
	 * 通过id删除店铺信息
	 * @param shopId
	 * 店铺id
	 * @return
	 * 删除结果
	 */
	public boolean deleteShopById(String shopId){
		result = false;
		if (shopId.trim().length() > 0) {
			result = shopUser.dao().findById(shopId).delete();
			return result;
		}
		return result;
	}
}
